{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cb387fbb",
   "metadata": {},
   "source": [
    "# 基于SpringBoot+Python的多语言前后端智能多人聊天系统（第1期）第2课书面作业\n",
    "\n",
    "学号：114498  \n",
    "\n",
    "**作业内容：**  \n",
    "分别用TensorFlow、Numpy完成四则运算和矩阵运算 （代码和截图）。  \n",
    "\n",
    "注：本次作业用jupyter notebook来实现，代码后直接有结果输出，因此不再额外截图了。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f1cd7f1",
   "metadata": {},
   "source": [
    "## 1. 用Tensorflow实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c0c47121",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.5.0\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "print(tf.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "463dd321",
   "metadata": {},
   "source": [
    "**注：本次作业的Tensorflow版本为2.5.0，因此教程视频中的tf.Session方法已经不适用，这里直接用“变量.numpy()”输出变量值。**  \n",
    "\n",
    "### 1.1 单个数的四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "57166791",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "65.82578\n"
     ]
    }
   ],
   "source": [
    "a=tf.constant(12.3)\n",
    "b=tf.Variable(2.34,\"b\")\n",
    "#加法\n",
    "c1=tf.add(a,b)\n",
    "#减法\n",
    "c2=tf.subtract(c1,2.5)\n",
    "#乘法\n",
    "c3=tf.multiply(c1,c2)\n",
    "#除法\n",
    "c4=tf.divide(c3,2.7)\n",
    "\n",
    "print(c4.numpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad7d35b1",
   "metadata": {},
   "source": [
    "### 1.2 数组的四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a849a223",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 43.500004  34.285713 455.      ]\n"
     ]
    }
   ],
   "source": [
    "alst=tf.constant([1.,2.,3.])\n",
    "blst=tf.Variable([2.,3.,4.],\"b\")\n",
    "#加法\n",
    "clst1=tf.add(alst,blst)\n",
    "#减法\n",
    "clst2=tf.subtract(clst1,[0.1,0.2,0.5])\n",
    "#乘法\n",
    "clst3=tf.multiply(clst1,clst2)\n",
    "#除法\n",
    "clst4=tf.divide(clst3,[0.2,0.7,0.1])\n",
    "\n",
    "print(clst4.numpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8edb72b",
   "metadata": {},
   "source": [
    "### 1.3 矩阵的四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e81d5490",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.0125     -0.03333333 -0.075     ]\n",
      " [-0.2         0.55555556  0.54545455]\n",
      " [ 0.175       0.13333333  0.1125    ]]\n"
     ]
    }
   ],
   "source": [
    "A=tf.constant(\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6],\n",
    "        [7,8,9]\n",
    "    ]\n",
    ")\n",
    "B=tf.Variable(\n",
    "    [\n",
    "        [9,8,7],\n",
    "        [6,4,5],\n",
    "        [3,2,1]\n",
    "    ],\n",
    "    'B'\n",
    ")\n",
    "\n",
    "#加法\n",
    "C1=tf.add(A,B)\n",
    "#减法\n",
    "C2=tf.subtract(A,B)\n",
    "#乘法\n",
    "C3=tf.multiply(C1,C2)\n",
    "#除法\n",
    "C4=tf.divide(A,C3)\n",
    "\n",
    "print(C4.numpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c409f955",
   "metadata": {},
   "source": [
    "### 1.4 矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9ef39d08",
   "metadata": {},
   "outputs": [],
   "source": [
    "A=tf.constant(\n",
    "    [\n",
    "        [1.,2.,3.],\n",
    "        [4.,5.,6.],\n",
    "        [7.,8.,9.]\n",
    "    ]\n",
    ")\n",
    "B=tf.Variable(\n",
    "    [\n",
    "        [9.,8.,7.],\n",
    "        [6.,4.,5.],\n",
    "        [3.,2.,1.]\n",
    "    ],\n",
    "    'B'\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "78649560",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 30.  22.  20.]\n",
      " [ 84.  64.  59.]\n",
      " [138. 106.  98.]]\n"
     ]
    }
   ],
   "source": [
    "#矩阵乘法\n",
    "mC1=tf.matmul(A,B)\n",
    "print(mC1.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "b2142ed2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 4. 7.]\n",
      " [2. 5. 8.]\n",
      " [3. 6. 9.]]\n"
     ]
    }
   ],
   "source": [
    "#矩阵转置\n",
    "mC2=tf.transpose(A)\n",
    "print(mC2.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "4bab9cbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.3333333   0.33333334  0.66666657]\n",
      " [ 0.49999997 -0.6666667  -0.1666665 ]\n",
      " [-0.          0.33333334 -0.6666667 ]]\n"
     ]
    }
   ],
   "source": [
    "#矩阵求逆\n",
    "mC3=tf.linalg.inv(B)\n",
    "print(mC3.numpy())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd2eed85",
   "metadata": {},
   "source": [
    "## 2. 用numpy完成\n",
    "上面的这个计算过程用numpy实现一下。  \n",
    "\n",
    "### 2.1 单个数的四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a53a266d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.19.5\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "print(np.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e69b171c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "65.82577777777777\n"
     ]
    }
   ],
   "source": [
    "na=np.array([12.3])\n",
    "nb=np.array([2.34])\n",
    "\n",
    "#加法\n",
    "nc1=np.add(na,nb)\n",
    "#减法\n",
    "nc2=np.subtract(nc1,2.5)\n",
    "#乘法\n",
    "nc3=np.multiply(nc1,nc2)\n",
    "#除法\n",
    "nc4=np.divide(nc3,2.7)\n",
    "\n",
    "print(nc4[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da53e991",
   "metadata": {},
   "source": [
    "这个计算结果与tensorflow计算一致。  \n",
    "\n",
    "### 2.2 数组的四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "14da1b75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 43.5         34.28571429 455.        ]\n"
     ]
    }
   ],
   "source": [
    "nalst=np.array([1.,2.,3.])\n",
    "nblst=np.array([2.,3.,4.])\n",
    "#加法\n",
    "nclst1=np.add(nalst,nblst)\n",
    "#减法\n",
    "nclst2=np.subtract(nclst1,[0.1,0.2,0.5])\n",
    "#乘法\n",
    "nclst3=np.multiply(nclst1,nclst2)\n",
    "#除法\n",
    "nclst4=np.divide(nclst3,[0.2,0.7,0.1])\n",
    "\n",
    "print(nclst4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fea4b314",
   "metadata": {},
   "source": [
    "这个计算结果也与tensorflow计算一致。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cdf2505",
   "metadata": {},
   "source": [
    "### 2.3 矩阵的四则运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "379ce449",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.0125     -0.03333333 -0.075     ]\n",
      " [-0.2         0.55555556  0.54545455]\n",
      " [ 0.175       0.13333333  0.1125    ]]\n"
     ]
    }
   ],
   "source": [
    "nA=np.matrix(\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6],\n",
    "        [7,8,9]\n",
    "    ]\n",
    ")\n",
    "\n",
    "nB=np.matrix(\n",
    "    [\n",
    "        [9,8,7],\n",
    "        [6,4,5],\n",
    "        [3,2,1]\n",
    "    ]\n",
    ")\n",
    "\n",
    "#加法\n",
    "nC1=np.add(nA,nB)\n",
    "#减法\n",
    "nC2=np.subtract(nA,nB)\n",
    "#乘法\n",
    "nC3=np.multiply(nC1,nC2)\n",
    "#除法\n",
    "nC4=np.divide(nA,nC3)\n",
    "\n",
    "print(nC4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8625bf9",
   "metadata": {},
   "source": [
    "### 2.4 矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8a245cef",
   "metadata": {},
   "outputs": [],
   "source": [
    "nA=np.matrix(\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6],\n",
    "        [7,8,9]\n",
    "    ]\n",
    ")\n",
    "\n",
    "nB=np.matrix(\n",
    "    [\n",
    "        [9,8,7],\n",
    "        [6,4,5],\n",
    "        [3,2,1]\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9daf088a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 30  22  20]\n",
      " [ 84  64  59]\n",
      " [138 106  98]]\n"
     ]
    }
   ],
   "source": [
    "#矩阵乘法\n",
    "nmC1=np.matmul(nA,nB)\n",
    "print(nmC1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "64e76750",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4 7]\n",
      " [2 5 8]\n",
      " [3 6 9]]\n"
     ]
    }
   ],
   "source": [
    "#矩阵转置\n",
    "nmC2=np.transpose(nA)\n",
    "print(nmC2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "0d4c9985",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.33333333  0.33333333  0.66666667]\n",
      " [ 0.5        -0.66666667 -0.16666667]\n",
      " [-0.          0.33333333 -0.66666667]]\n"
     ]
    }
   ],
   "source": [
    "#矩阵求逆\n",
    "nmC2=np.linalg.inv((nB))\n",
    "print(nmC2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72f8efd8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
